package org.example.pcbcircuitboard.controller;

import org.eclipse.paho.client.mqttv3.IMqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Base64;

@RestController
@RequestMapping("/mqtt")
public class MqttController {
    @Value("${mqtt.topic}")
    private String topic;
    @Autowired
    private IMqttClient iMqttClient;

    /**
     * 测试发送 MQTT 消息
     * @return ok
     * @throws MqttException Mqtt 异常
     */
    @GetMapping("/publish")
    public String publish() throws MqttException {
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setPayload("Hello MQTT".getBytes());
        mqttMessage.setQos(1);
        mqttMessage.setRetained(true);
        iMqttClient.publish("pcb/control",mqttMessage);
        return "ok";
    }

    /**
     * 测试发送 MQTT 消息 处理 JSON 数据
     * @return ok
     * @throws MqttException Mqtt 异常
     */
    @GetMapping("/test-send")
    public String testSend() throws MqttException, IOException {
        // 读取本地图片文件
        File imageFile = new File("C:\\Users\\33416\\Desktop\\VSCODE\\board-inspection\\board-vis\\src\\assets\\img\\dlb.jpg");
        FileInputStream fileInputStream = new FileInputStream(imageFile);

        // 将图片文件转换为字节数组
        byte[] imageBytes = new byte[(int) imageFile.length()];
        fileInputStream.read(imageBytes);

        // 将字节数组转换为 Base64 编码字符串
        String base64Image = Base64.getEncoder().encodeToString(imageBytes);

        // 关闭文件输入流
        fileInputStream.close();
        // JSON 数据
        String jsonData = "{"
                + "\"timestamp\": \"2023-07-07T16:45:30Z\","
                + "\"has_defects\": true,"
                + "\"defects\": ["
                + "  {\"type\": 1, \"confidence\": 0.92, \"location\": \"B5\"},"
                + "  {\"type\": 8, \"confidence\": 0.92, \"location\": \"B5\"},"
                + "  {\"type\": 6, \"confidence\": 0.92, \"location\": \"B5\"},"
                + "  {\"type\": 2, \"confidence\": 0.92, \"location\": \"B5\"},"
                + "  {\"type\": 5, \"confidence\": 0.87, \"location\": \"C3\"}"
                + "],"
                + "\"location\": {\"rack\": 1, \"slot\": 3},"
                + "\"product\": {"
                + "    \"model\": \"PCB-A558\","
                + "    \"batch\": \"B20230706\","
                + "    \"serial\": \"SN-007654\""
                + "  },"
                + "\"image\": {"
                + "  \"thumbnail\": \"" + base64Image + "\","
                + "  \"width\": 320, \"height\": 240, \"format\": \"jpeg\""
                + "}"
                + "}";

        // 将 JSON 数据封装到 MqttMessage
        MqttMessage mqttMessage = new MqttMessage(jsonData.getBytes());
        mqttMessage.setQos(0);
        mqttMessage.setRetained(true);

        // 发布消息
        iMqttClient.publish(topic, mqttMessage);

        return "ok";
    }
    @GetMapping("/test2-send")
    public String testSend2() throws MqttException {
        // JSON 数据
        String jsonData = "{"
                + "\"timestamp\": \"2023-07-07T16:45:30Z\","
                + "\"has_defects\": false,"
                + "\"defects\": ["
                + "],"
                + "\"location\": {\"rack\": 1, \"slot\": 3},"
                + "\"product\": {"
                + "    \"model\": \"PCB-F700\","
                + "    \"batch\": \"B20230706\","
                + "    \"serial\": \"SN-007654\""
                + "  },"
                + "\"image\": {"
                + "  \"thumbnail\": \"Base64编码\","
                + "  \"width\": 320, \"height\": 240, \"format\": \"jpeg\""
                + "}"
                + "}";

        // 将 JSON 数据封装到 MqttMessage
        MqttMessage mqttMessage = new MqttMessage(jsonData.getBytes());
        mqttMessage.setQos(0);
        mqttMessage.setRetained(true);

        // 发布消息
        iMqttClient.publish(topic, mqttMessage);

        return "ok";
    }
}
